#include "comm.h"
using namespace std;



bool MakeFile()
{
    int n = mkfifo(FILENAME, 0666);
    if (n < 0)
    {
        cerr << "errno: " << errno << ",cerrstring : " << strerror(errno) << endl;
        return false;
    }
    cout<< "mkfifo success.....read"<<endl;
    return true;
}

int main()
{
    // 创建管道文件
Start:
    int fd = open(FILENAME, O_RDONLY);
    if (fd < 0)
    {
        //cerr << "errno: " << errno << ",cerrstring : " << strerror(errno) << endl;
        if(MakeFile()) //没有文件就创建文件重新打开goto 
        {
            goto Start;
        }
        else{
            return 1;
        }
    }
    cout<< "open fifo success.....read"<<endl;
    char buff[1024];
    while (true)
    {
        ssize_t size = read(fd, buff, sizeof(buff) - 1);
        if (size > 0)
        {
            buff[size] = 0;
            cout << "client Say#" << buff << endl;
        }
    }
    close(fd);
    cout<< "close success.....read"<<endl;
    return 0;
}